
import Vue from 'vue'
import router from '../../../src/router'
import VueResource from 'vue-resource'
Vue.use(VueResource)
import { Loading } from 'element-ui';//菊花
// let service_url='http://60.173.247.165:8081/yx-stamper-api'
// let service_url = '/apis'//国盾专用
// let service_Img = ''//国盾专用
// let service_url = 'http://114.214.170.24:8080'//巧生本地测试
// let service_Img = 'http://114.214.170.24:9000'//巧生本地测试
// let service_url = 'http://210.45.123.55:8080'//周海峰本地测试
let service_url = 'http://223.247.138.196:8082'//天翼云

// let service_Img = ''//周海峰本地测试
// let service_url='http://yx.qstamper.com:8080'//浪潮峰测试
let service_Img="http://yx.qstamper.com"//浪潮图片前缀
// let service_url='http://192.168.220.154:8080'//航信测试
// let service_Img="http://192.168.220.154:9000"//航信测试
function doGet(url,req,successCallback, errorCallback){
	//菊花
	let loadingInstance = Loading.service({
		lock: true,
		text: '努力加载中....',
		spinner: 'el-icon-loading',
		background: 'rgba(0, 0, 0, 0.7)'
		});
	Vue.http.get(service_url+url,req).then(function(response){
		//关闭菊花
		loadingInstance.close();
		// 响应成功回调
		successCallback(response.data.data);
	}, function(error){
		//关闭菊花
		loadingInstance.close();
		// 响应错误回调
		errorCallback(error)
	});
}
function doPost(url,req, successCallback){
		//菊花
		let loadingInstance=""
		if(req.keyword){
			
		}else{
			loadingInstance = Loading.service({
				lock: true,
				text: '努力加载中....',
				spinner: 'el-icon-loading',
				background: 'rgba(0, 0, 0, 0.7)'
			});
		}
		Vue.http.post(service_url+url,req,{emulateJSON:true}).then(function(response){
			//关闭菊花
			try{
				loadingInstance.close();
			}catch(e){
				//TODO handle the exception
			}
				//大于600跳登录
				if(response.data.code>600||response.data.code==600){
					//未登录,跳转登录页面
					Vue.prototype.$message({
						message: response.data.msg,
						type: 'warning'
					});
					//关闭菊花
					try{
						loadingInstance.close();
					}catch(e){
						//TODO handle the exception
					}
					router.push({name:'login',path:'/login'})
				}
				if(response.data.code!=200){
					//未登录,跳转登录页面
					Vue.prototype.$message({
						message: response.data.msg,
						type: 'warning'
					});
				}
				if(response.data.code==599){
					router.push({name:'myInfo',path:'/myInfo'})
				}
				// 响应成功回调
				successCallback(response.data);
			
			
		}, function(error){
			//关闭菊花
			try{
				loadingInstance.close();
			}catch(e){
				//TODO handle the exception
			}
		});
}
// //限制上传文件格式第一个传上传文件名，第二个限制类型
function restrictFormatUploaded(name,types){
	let mun=name.indexOf('.')
	let sliceName=name.slice(mun,name.length)
	// let filetypes =["jpg","jpeg","png","rar","txt","zip","doc","ppt","xls","pdf","docx","xlsx","JPG","JPEG","PNG","RAR","TXT","ZIP","DOC","PPT","XLS","PDF","DOCX","XLSX"];
	let filetypes =types
	let arr=filetypes.filter(function(res){
		if(sliceName.includes(res)){
			return res;
		}
	})
	let onoff=false
	if(arr.length>0){
		onoff=true
	}else{
		onoff=false
	}
	return onoff;
	
}
// //获取时间
function getBeforeDate(n){//n为你要传入的参数，当前为0，前一天为-1，后一天为1
	let date = new Date();
	let year,month,day,s;
	date.setDate(date.getDate()+n);
	year = date.getFullYear();
	month = date.getMonth()+1;
	day = date.getDate() ;
	s = year + '/' + ( month < 10 ? ( '0' + month ) : month ) + '/' + ( day < 10 ? ( '0' + day ) : day);
	return s ;
}


export default{
	doGet:doGet,
	doPost:doPost,
	getBeforeDate:getBeforeDate,
	service_url:service_url,
	service_Img:service_Img,
	restrictFormatUploaded:restrictFormatUploaded
}